<template>
	<view class="container">
		<!-- 顶部导航栏 -->
		<view class="header">
			<view class="back-icon" @click="goBack">
				<text class="iconfont">←</text>
				<text class="back-text">返回</text>
			</view>
			<view class="title">我的订单</view>
			<view class="website">read.yangtse.com</view>
		</view>
		
		<!-- 订单列表 -->
		<view class="order-list" v-if="orderList.length > 0">
			<view class="order-item" v-for="(item, index) in orderList" :key="index">
				<view class="order-header">
					<view class="order-id">订单号：{{item.id}}</view>
					<view class="order-status" :class="[getStatusClass(item.status)]">{{getStatusText(item.status)}}</view>
				</view>
				<view class="order-content">
					<view class="order-info">
						<view class="info-item">
							<text class="info-label">分类：</text>
							<text class="info-value">{{item.category}} - {{item.subCategory}}</text>
						</view>
						<view class="info-item">
							<text class="info-label">刊登日期：</text>
							<text class="info-value">{{item.publishDate}}</text>
						</view>
						<view class="info-item">
							<text class="info-label">刊登天数：</text>
							<text class="info-value">{{item.publishDays}}天</text>
						</view>
						<view class="info-item">
							<text class="info-label">创建时间：</text>
							<text class="info-value">{{formatDate(item.createTime)}}</text>
						</view>
					</view>
					<view class="order-price">
						<text class="price-label">价格：</text>
						<text class="price-value">¥{{item.price}}</text>
					</view>
				</view>
				<view class="order-actions">
					<view class="action-btn detail-btn" @click="viewOrderDetail(item)">
						<text>查看详情</text>
					</view>
					<view class="action-btn contact-btn" @click="contactCustomerService">
						<text>联系客服</text>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 空订单提示 -->
		<view class="empty-order" v-else>
			<image src="/static/empty-order.png" mode="aspectFit" class="empty-image"></image>
			<text class="empty-text">暂无订单记录</text>
			<view class="go-publish-btn" @click="goToPublish">
				<text>去登报</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orderList: [],
				statusMap: {
					0: '待支付',
					1: '已支付',
					2: '已取消',
					3: '已完成'
				}
			}
		},
		onLoad() {
			this.loadOrderList();
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			loadOrderList() {
				uni.showLoading({
					title: '加载中...'
				});
				
				// 调用云函数获取订单列表
				uniCloud.callFunction({
					name: 'getOrderList',
					data: {}
				}).then(res => {
					uni.hideLoading();
					console.log('获取订单列表成功：', res);
					
					if (res.result && res.result.code === 0) {
						this.orderList = res.result.data || [];
					} else {
						// 模拟数据，实际开发中应该从服务器获取
						this.orderList = [
							{
								id: 'AD20230001',
								category: '个人证件',
								subCategory: '身份证',
								publishDate: '20230601',
								publishDays: 1,
								price: 50,
								status: 3,
								createTime: new Date('2023-06-01')
							},
							{
								id: 'AD20230002',
								category: '遗失声明',
								subCategory: '',
								publishDate: '20230610',
								publishDays: 3,
								price: 120,
								status: 0,
								createTime: new Date('2023-06-09')
							}
						];
					}
				}).catch(err => {
					uni.hideLoading();
					console.error('获取订单列表失败：', err);
					
					// 模拟数据，实际开发中应该从服务器获取
					this.orderList = [
						{
							id: 'AD20230001',
							category: '个人证件',
							subCategory: '身份证',
							publishDate: '20230601',
							publishDays: 1,
							price: 50,
							status: 3,
							createTime: new Date('2023-06-01')
						},
						{
							id: 'AD20230002',
							category: '遗失声明',
							subCategory: '',
							publishDate: '20230610',
							publishDays: 3,
							price: 120,
							status: 0,
							createTime: new Date('2023-06-09')
						}
					];
					
					uni.showToast({
						title: '获取订单列表失败',
						icon: 'none'
					});
				});
			},
			formatDate(date) {
				if (!date) return '';
				if (typeof date === 'string') {
					date = new Date(date);
				}
				return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
			},
			getStatusClass(status) {
				const classMap = {
					0: 'status-pending',
					1: 'status-paid',
					2: 'status-cancelled',
					3: 'status-completed',
					4: 'status-processing'
				};
				return classMap[status] || '';
			},
			getStatusText(status) {
				return this.statusMap[status] || '未知状态'
			},
			viewOrderDetail(order) {
				// 跳转到订单详情页
				uni.navigateTo({
					url: `/pages/my/order-detail?id=${order._id}`
				});
			},
			contactCustomerService() {
				uni.makePhoneCall({
					phoneNumber: '13776769686'
				});
			},
			goToPublish() {
				uni.switchTab({
					url: '/pages/index/index'
				});
			}
		}
	}
</script>

<style lang="scss">
	.container {
		min-height: 100vh;
		background-color: #f8f8f8;
		padding-bottom: 40rpx;
	}
	
	.header {
		background: linear-gradient(135deg, #2ecc71, #27ae60);
		color: #fff;
		padding: 40rpx 30rpx;
		display: flex;
		align-items: center;
		position: relative;
		box-shadow: 0 2px 10px rgba(0,0,0,0.1);
		
		.back-icon {
			display: flex;
			align-items: center;
			background-color: rgba(255, 255, 255, 0.2);
			padding: 10rpx 20rpx;
			border-radius: 30rpx;
			margin-right: 10rpx;
			
			.iconfont {
				font-size: 36rpx;
				font-weight: bold;
			}
			
			.back-text {
				font-size: 28rpx;
				margin-left: 10rpx;
			}
			
			&:active {
				background-color: rgba(255, 255, 255, 0.3);
			}
		}
		
		.title {
			flex: 1;
			text-align: center;
			font-size: 34rpx;
			font-weight: 500;
		}
		
		.website {
			position: absolute;
			bottom: 10rpx;
			right: 30rpx;
			font-size: 24rpx;
			opacity: 0.8;
		}
	}
	
	.order-list {
		padding: 20rpx;
	}
	
	.order-item {
		background: #fff;
		border-radius: 12rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2px 8px rgba(0,0,0,0.05);
		overflow: hidden;
		
		.order-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 30rpx;
			border-bottom: 1px solid #f5f5f5;
			
			.order-id {
				font-size: 28rpx;
				color: #666;
			}
			
			.order-status {
				font-size: 26rpx;
				font-weight: 500;
				
				&.status-pending {
					color: #FFA500;
				}
				
				&.status-paid {
					color: #4CAF50;
				}
				
				&.status-processing {
					color: #9b59b6;
				}
				
				&.status-completed {
					color: #2196F3;
				}
				
				&.status-cancelled {
					color: #999999;
				}
			}
		}
		
		.order-content {
			padding: 20rpx 30rpx;
			
			.order-info {
				.info-item {
					display: flex;
					margin-bottom: 10rpx;
					
					.info-label {
						font-size: 26rpx;
						color: #999;
						width: 160rpx;
					}
					
					.info-value {
						font-size: 26rpx;
						color: #333;
						flex: 1;
					}
				}
			}
			
			.order-price {
				display: flex;
				justify-content: flex-end;
				align-items: center;
				margin-top: 20rpx;
				
				.price-label {
					font-size: 28rpx;
					color: #666;
				}
				
				.price-value {
					font-size: 32rpx;
					color: #e74c3c;
					font-weight: 500;
				}
			}
		}
		
		.order-actions {
			display: flex;
			justify-content: flex-end;
			padding: 20rpx 30rpx;
			border-top: 1px solid #f5f5f5;
			
			.action-btn {
				padding: 10rpx 30rpx;
				border-radius: 30rpx;
				font-size: 26rpx;
				margin-left: 20rpx;
				
				&.detail-btn {
					background: #f5f5f5;
					color: #666;
					
					&:active {
						background: #f0f0f0;
					}
				}
				
				&.contact-btn {
					background: #2ecc71;
					color: #fff;
					
					&:active {
						opacity: 0.9;
					}
				}
			}
		}
	}
	
	.empty-order {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 100rpx 0;
		
		.empty-image {
			width: 200rpx;
			height: 200rpx;
			margin-bottom: 30rpx;
		}
		
		.empty-text {
			font-size: 28rpx;
			color: #999;
			margin-bottom: 40rpx;
		}
		
		.go-publish-btn {
			padding: 20rpx 60rpx;
			background: #2ecc71;
			color: #fff;
			border-radius: 40rpx;
			font-size: 28rpx;
			
			&:active {
				opacity: 0.9;
			}
		}
	}
</style> 